package com.zlp.mall.modules.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlp.mall.common.api.Pager;
import com.zlp.mall.common.util.BeanToUtils;
import com.zlp.mall.modules.cms.dto.resp.SubjectResp;
import com.zlp.mall.modules.cms.mapper.SubjectMapper;
import com.zlp.mall.modules.cms.model.Subject;
import com.zlp.mall.modules.cms.service.SubjectService;
import com.zlp.mall.modules.pms.dto.resp.ProductResp;
import com.zlp.mall.modules.pms.model.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 专题表 服务实现类
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-10-08
 */
@Service
@Slf4j(topic = "SubjectServiceImpl")
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Override
    public Pager<SubjectResp> getPageList(String keyword, Integer pageNum, Integer pageSize) {

        log.info("getPageList.req keyword={},pageNum={},pageSize={}",keyword,pageNum,pageSize);
        Page pages =new Page(pageNum,pageSize);
        LambdaQueryWrapper<Subject> lambdaWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(keyword)) {
            lambdaWrapper.like(Subject::getTitle, keyword);
        }
        lambdaWrapper.orderByDesc(Subject::getId);
        Page page = this.page(pages, lambdaWrapper);
        List records = page.getRecords();
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            List<SubjectResp> subjectResps = BeanToUtils.entityToList(records, SubjectResp.class);
            return new Pager(subjectResps,  page.getTotal(), pageSize, pageNum);
        }
        return new Pager<>();

    }

    @Override
    public List<SubjectResp> listAll() {

        log.info("listAll");
        List<Subject> list = this.list();
        List<SubjectResp> subjectRespList = BeanToUtils.entityToList(list, SubjectResp.class);
        return subjectRespList;
    }
}
